/*
 * libroxsd - a portable, lightweight XSD validation library
 * Copyright (C) 2012  Emmanuel Deloget
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#ifndef roxsd_compiler_h_included
#define roxsd_compiler_h_included

#include <roxml.h>
#include "roxsd-list.h"
#include "roxsd-error.h"
#include "roxsd-compiler-xsdtype.h"

/** @brief XSD compiler info
 *
 * @typedef xsdcompiler_t
 */
typedef struct xsdcompiler_s xsdcompiler_t;

/** @brief XSD compiler info
 *
 * @struct xsdcompiler_s
 */
struct xsdcompiler_s {
	list_define_header();		/**< list header */
	xsdtype_t *types;		/**< list of named element/attribute types */
	xsdtype_t *roots;		/**< list of named root elements */
};

/** @brief XSD validation environment
 *
 * @struct xsdenv_s
 */
struct xsdenv_s {
	xsdcompiler_t *xsdcompiler;	/**< XSD compiler */
	xsdcompiler_t *compilers;	/**< list of compilers */
	xsderr_t *errors;		/**< error descriptions */
};

/** @brief Compile the XML schema
 *
 * @param xsdenv XSD validation environment
 * @param node XSD root node
 * @return 0 if ok, < 0 if any error occured (and errno is set)
 */
int roxsdi_compile_schema(xsdenv_t *xsdenv, node_t *node);

/** @brief Register a new type in the compiler
 *
 * @param compiler XSD compiler
 * @param t XSD type
 * @return 0 if OK, < 0 on error
 */
int roxsdi_compiler_register_type(xsdcompiler_t *compiler, xsdtype_t *t);

/** @brief Register a root node in the compiler
 *
 * @param compiler XSD compiler
 * @param ename root element name
 * @param etype root element type
 * @return 0 if OK, < 0 on error
 *
 * @a etype shall exist in the compiler type list before the compiler
 * is used to validate an XML file - otherwise, validation will fail.
 */
int roxsdi_compiler_register_root(xsdcompiler_t *compiler, char *ename, char *etype);

/** @brief Create a new compiler
 *
 * @return a newly allocated compiler or NULL on error
 */
xsdcompiler_t *roxsdi_compiler_new(void);

/** @brief Free compiler resources
 *
 * @param compiler XSD compiler
 */
void roxsdi_compiler_free(xsdcompiler_t *compiler);

#if !defined(NDEBUG)
/**@brief Check the compiler definition
 *
 * @param compiler the XSD compiler
 * @return 0 on success, < 0 on error
 *
 * This function check if the compiler is correctly and fully defined;
 * i.e. there is no missing type, all defined elements have a type and so
 * on. This is useful for debugging, but the function is a pain in a
 * production environment.
 */
int roxsdi_compiler_check(xsdcompiler_t *compiler);
#endif

#endif /* roxsd_compiler_h_included */
